package com.cloud.logger.zchain.zchainserver.config;

import com.cloud.logger.zchain.zchainserver.mongo.BaseMongoRepositoryImpl;
import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * User: wcy
 * Date: 2017/10/30
 * Time: 15:19
 */
@Configuration
@ComponentScan({"com.cloud.logger.zchain.zchainserver"})
@EnableMongoRepositories(repositoryBaseClass = BaseMongoRepositoryImpl.class, basePackages = "com.cloud.logger.zchain.zchainserver.mongo")
public class MongoConfiguration {
    @Autowired
    private MongoProperties mongoProperties;

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception{
        return new SimpleMongoDbFactory(new MongoClient(mongoProperties.getHost(),mongoProperties.getPort()),mongoProperties.getDatabase());
    }

    public @Bean MongoTemplate mongoTemplate() throws Exception{
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }
}
